function loadXMLSettings(handles,path)

% This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2013. Jose Maria Garcia-Valdecasas Bernal

fName=fullfile(path,'settings.xml');

if ~exist(fName,'file')
    handles.DisplayWidget.defaultSettings();
    return;
end

xDoc=xmlread(fName);
%document root node
docNode=xDoc.getDocumentElement;
%clean the xml file with empty nodes (#text).
removeIndentNodes(docNode.getChildNodes);

childNodes=docNode.getChildNodes;
for j=0:childNodes.getLength()-1
    child=char(childNodes.item(j).getNodeName());
    switch child
        case 'SWAN_Path'
            SWANPath=char(childNodes.item(j).getTextContent());
            if ~isempty(SWANPath)
                handles.DisplayWidget.trickSWANPath(SWANPath);
            end
        
        case 'Axes'
            props=childNodes.item(j).getChildNodes();
            for k=0:props.getLength()-1
                prop=props.item(k);
                %retrieve name of the property
                name=char(prop.getNodeName());
                %retrieve the value of the property
                value=char(prop.getTextContent());
                value=txt2data(value);
                handles.DisplayWidget.setAxProp(name,value);
            end
            
        case 'CGrids'
            props=childNodes.item(j).getChildNodes();
            for k=0:props.getLength()-1
                prop=props.item(k);
                %retrieve name of the property
                name=char(prop.getNodeName());
                %retrieve kind of grid
                kind=char(prop.getAttribute('kind'));
                %retrieve the value of the property
                value=str2num(char(prop.getTextContent()));
                handles.DisplayWidget.setCGridColor(kind,name,value);
            end
            
        case 'CPoints'
            props=childNodes.item(j).getChildNodes();
            for k=0:props.getLength()-1
                prop=props.item(k);
                %retrieve name of the property
                name=char(prop.getNodeName());
                %retrieve the value of the property
                value=char(prop.getTextContent());
                value=txt2data(value);
                handles.DisplayWidget.setCPointProp(name,value);
            end
            
        case 'IGrids'
            grids=childNodes.item(j).getChildNodes();
            for k=0:grids.getLength()-1
                grid=grids.item(k);
                %retrieve name of the grid
                name=char(grid.getNodeName());
                props=grid.getChildNodes();
                for l=0:props.getLength()-1
                    prop=props.item(l);
                    %retrieve name of the property
                    propName=char(prop.getNodeName());
                    %retrieve the value of the property
                    value=char(prop.getTextContent());
                    value=txt2data(value);
                    handles.DisplayWidget.setGridColormap(name,...
                        propName,value(1,:),value(2,:));
                end
            end
    end
end

end

function data=txt2data(txt)

if length(regexp(txt,'[0-9;\.\s]'))==length(txt)
    %all data in txt is numeric, white space ';' or dot (numbers!)
    data=str2num(txt);
else
    %if it's not numeric, it must be a string, so it's ok.
    data=txt;
end

end